package com.qining618.cms.ucenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qining618.cms.ucenter.domain.UserAuth;
import com.qining618.cms.ucenter.enums.UserAuthTypeEnum;
import com.qining618.cms.ucenter.mapper.UserAuthMapper;
import com.qining618.cms.ucenter.service.UserAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 授权用户 服务实现类
 * </p>
 *
 * @author qining
 * @date 2019-09-23
 */
@Transactional(readOnly = true, rollbackFor = Exception.class)
@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuth> implements UserAuthService {

    @Override
    public UserAuth getWechatMiniUserAuth(String appid, String openid) {
        return this.baseMapper.selectOne(new QueryWrapper<UserAuth>().lambda()
                .eq(UserAuth::getType, UserAuthTypeEnum.WECHAT_MINI)
                .eq(UserAuth::getAppid, appid)
                .eq(UserAuth::getOpenid, openid));
    }

    @Override
    public UserAuth getWechatUnionidUserAuth(String unionid) {
        IPage<UserAuth> pageInfo = this.baseMapper.selectPage(new Page<>(1, 1),
                new QueryWrapper<UserAuth>()
                        .lambda()
                        .in(UserAuth::getType, UserAuthTypeEnum.WECHAT_MINI)
                        .eq(UserAuth::getUnionid, unionid));
        if (pageInfo.getRecords().size() > 0) {
            return pageInfo.getRecords().get(0);
        }
        return null;
    }

}
